#!/usr/bin/env bash

__rvm_select_rbx_nightly()
{
  (( ${rvm_nightly_flag:=0} == 1 )) || return 0

  \typeset org_rvm_ruby_patch_level _rvm_ruby_name
  if [[ "$rvm_ruby_version" == head ]]
  then rvm_ruby_version=""
  fi
  rvm_debug "searching for binary rbx ${rvm_ruby_version:-}${rvm_ruby_version:+-}${rvm_ruby_patch_level}*${rvm_ruby_name:+-}${rvm_ruby_name:-}"
  org_rvm_ruby_patch_level="$rvm_ruby_patch_level"
  _rvm_ruby_name="${rvm_ruby_name:-${detected_rvm_ruby_name:-}}"
  rvm_ruby_patch_level="$(
    __list_remote_all |
      __rvm_grep ${rvm_ruby_version:-}${rvm_ruby_version:+-}${org_rvm_ruby_patch_level}.*${_rvm_ruby_name:+-}${_rvm_ruby_name:-} |
      __rvm_tail -n 1
  )"
  [[ -n "${rvm_ruby_patch_level:-}" ]] ||
  {
    rvm_error "Could not find rbx binary '${rvm_ruby_version:-}${rvm_ruby_version:+-}${org_rvm_ruby_patch_level}*${rvm_ruby_name:+-}${rvm_ruby_name:-}' binary release."
    return 1
  }
  rvm_ruby_patch_level="${rvm_ruby_patch_level##*/}"
  rvm_ruby_patch_level="${rvm_ruby_patch_level%.tar.*}"
  if
    [[ -z "${rvm_ruby_version:-}" ]]
  then
    rvm_ruby_patch_level="${rvm_ruby_patch_level#rubinius-}"
    rvm_ruby_version="${rvm_ruby_patch_level%%-*}"
  fi
  if
    [[ -z "${rvm_ruby_name:-}" ]]
  then
    rvm_ruby_name="${rvm_ruby_patch_level##*-}"
  fi
  rvm_ruby_patch_level="${rvm_ruby_patch_level##*${org_rvm_ruby_patch_level}}"
  rvm_ruby_patch_level="${rvm_ruby_patch_level%%-*}"
  rvm_ruby_patch_level="${org_rvm_ruby_patch_level}${rvm_ruby_patch_level}"
  rvm_ruby_string="rubinius-${rvm_ruby_version}-${rvm_ruby_patch_level}-${rvm_ruby_name}"
  rvm_debug "detected rbx ${rvm_ruby_string}"
  rvm_verify_downloads_flag=1

  true # for OSX
}

__rvm_select_rbx_compatibility_branch()
{
  case "${rvm_ruby_version}" in
    (2.0pre)      rvm_ruby_repo_branch="master"              ;;
    (2.0.testing) rvm_ruby_repo_branch="${rvm_ruby_version}" ;;
  esac

  if
    [[ ${rvm_19_flag:-0} == 1 ]]
  then
    rvm_ruby_repo_branch="1.9.3"
    rvm_head_flag=1
  elif
    [[ ${rvm_18_flag:-0} == 1 ]]
  then
    rvm_ruby_repo_branch="1.8.7"
    rvm_head_flag=1
  fi

  true # for OSX
}

__rvm_select_interpreter_rbx()
{
  __rvm_select_rbx_nightly || return $?

  rvm_ruby_interpreter="rbx"

  __rvm_select_rbx_compatibility_branch

  if
    (( ${rvm_head_flag:=1} == 0 )) &&
    [[ -z "${rvm_ruby_repo_branch:-}" ]] &&
    [[ "${rvm_ruby_version}" != "head" ]]
  then
    if
      __rvm_version_compare "${rvm_ruby_version}" -ge "2.0.0"
    then
      rbx_url="$( __rvm_db "rbx_url_2.0_and_newer" )"
      rvm_archive_extension="tar.bz2"
      rvm_ruby_package_file="rubinius-${rvm_ruby_version}"
      rvm_ruby_url="${rbx_url}/${rvm_ruby_package_file}.${rvm_archive_extension}"
    else
      rbx_url=${rbx_url:-$(__rvm_db "rbx_url")}
      rvm_archive_extension="tar.gz"
      rvm_ruby_package_file="rubinius-${rvm_ruby_version}"
      rvm_ruby_url="${rbx_url}/$rvm_ruby_package_file.${rvm_archive_extension}"
    fi
  else
    rvm_ruby_repo_url=${rvm_rbx_repo_url:-$(__rvm_db "rbx_repo_url")}
    rvm_head_flag=1
    rvm_ruby_patch_level=""
    rvm_ruby_tag="${rvm_ruby_version:+v}${rvm_ruby_version:-}"
    rvm_ruby_version="head"
    rvm_disable_binary_flag=1
  fi

  if [[ -n "${rvm_rbx_opt:-}" ]]
  then export RBXOPT="${RBXOPT:=${rvm_rbx_opt}}"
  fi
  true # for OSX
}

__rvm_select_interpreter_rubinius()
{
  __rvm_select_interpreter_rbx || return $?
}

__rvm_select_macruby_nightly_selected()
{
  \typeset __string_version
  __string_version="${rvm_ruby_version//./-}"
  __string_version="${__string_version#nightly_}"

  rvm_ruby_package_name="${rvm_ruby_interpreter}_nightly-${__string_version}"
  rvm_ruby_package_file="$rvm_ruby_package_name.pkg"
}

__rvm_select_macruby_nightly_detect()
{
  \typeset __string_version
  rvm_ruby_version="$(
    __rvm_curl -s "$rvm_ruby_url" |
    __rvm_grep -oE "<a href=\"macruby_nightly-[^<]+\.pkg</a>" |
    __rvm_awk -F"[<>]" '{print $3}' |
    __rvm_version_sort |
    __rvm_tail -n 1
  )"
  [[ -n "${rvm_ruby_version}" ]] ||
  {
    rvm_error "Could not find MacRuby nightly binary."
    return 1
  }
  rvm_ruby_package_file="${rvm_ruby_version}"
  rvm_ruby_package_name="${rvm_ruby_package_file%.pkg}"

  __string_version="${rvm_ruby_package_name#macruby_nightly-}"
  __string_version="${__string_version//-/.}"

  rvm_ruby_version="nightly_${__string_version}"
  rvm_ruby_string="macruby-${rvm_ruby_version}${rvm_ruby_name:+-}${rvm_ruby_name:-}"

  true # for OSX
}

__rvm_select_macruby_nightly()
{
  __rvm_db "macruby_nightly_url" "rvm_ruby_url"

  case "${rvm_ruby_version:-}" in
    (nightly_*)
      __rvm_select_macruby_nightly_selected
      ;;
    (*)
      __rvm_select_macruby_nightly_detect
      ;;
  esac

  rvm_ruby_url+="/${rvm_ruby_package_file}"
  rvm_verify_downloads_flag=1
  rvm_debug "selected macruby $rvm_ruby_string => $rvm_ruby_url"

  true # for OSX
}

__rvm_select_interpreter_macruby()
{
  if
    [[ "Darwin" == "${_system_type}" ]]
  then
    rvm_ruby_package_name="${rvm_ruby_interpreter}-${rvm_ruby_version}"
    if
      (( ${rvm_head_flag:=0} == 1 ))
    then
      rvm_ruby_version=""
      rvm_ruby_tag=""
      rvm_ruby_revision="head"
      __rvm_db "macruby_repo_url" "rvm_ruby_repo_url"
      rvm_ruby_url="$rvm_ruby_repo_url"
      rvm_disable_binary_flag=1
    elif
      [[ "${rvm_ruby_version:-}" == *"nightly"* ]]
    then
      __rvm_select_macruby_nightly
    elif
      [[ -n "${rvm_ruby_version:-}" ]]
    then
      __rvm_db "macruby_${rvm_ruby_version}_url" "rvm_ruby_url"
      [[ -n "${rvm_ruby_url:-}" ]] || __rvm_db "macruby_url" "rvm_ruby_url"
      rvm_ruby_package_name="MacRuby%20${rvm_ruby_version}.zip"
      rvm_ruby_package_file="$rvm_ruby_package_name"
      rvm_ruby_url="$rvm_ruby_url/$rvm_ruby_package_name"
    else
      __rvm_db "macruby_version" "rvm_ruby_version"
      __rvm_db "macruby_url" "rvm_ruby_url"
      rvm_ruby_package_name="MacRuby%20${rvm_ruby_version}.zip"
      rvm_ruby_package_file="$rvm_ruby_package_name"
      rvm_ruby_url="$rvm_ruby_url/$rvm_ruby_package_name"
    fi
    rvm_ruby_patch_level=""
  else
    rvm_error "MacRuby can only be installed on a Darwin OS."
  fi
  true # for OSX
}

__rvm_select_interpreter_jruby()
{
  rvm_ruby_patch_level=""
  rvm_ruby_repo_url="${rvm_ruby_repo_url:-$(__rvm_db "jruby_repo_url")}"
  rvm_ruby_url="${rvm_ruby_repo_url:-$(__rvm_db "jruby_repo_url")}"
  if (( ${rvm_head_flag:=0} == 1 ))
  then
    (( ${rvm_remote_flag:-0} == 1 )) || rvm_disable_binary_flag=1
    rvm_ruby_version="head"
  else
    if (( ${rvm_18_flag:-0} || ${rvm_19_flag:-0} || ${rvm_20_flag:-0} || ${#rvm_patch_names[@]} ))
    then rvm_disable_binary_flag=1
    fi
    rvm_ruby_version="${rvm_ruby_version:-"$(__rvm_db "jruby_version")"}"
    rvm_ruby_tag="${rvm_ruby_tag:-${rvm_ruby_version}}"
  fi

  alias jruby_ng="jruby --ng"
  alias jruby_ng_server="jruby --ng-server"

  true # for OSX
}

__rvm_truffleruby_set_version()
{
  if (( ${rvm_head_flag:=0} == 1 ))
  then
    rvm_ruby_version="head"
    truffleruby_version="head"
  else
    rvm_ruby_version="${rvm_ruby_version:-$(__rvm_db "truffleruby_version")}"
    truffleruby_version="${rvm_ruby_version}${rvm_ruby_patch_level:+-}${rvm_ruby_patch_level:-}"
  fi

  true # for OSX
}

__rvm_truffleruby_set_rvm_ruby_url()
{
  case "${_system_type}" in
    Linux) platform="linux" ;;
    Darwin) platform="macos" ;;
    *) rvm_error "TruffleRuby does not support ${_system_type} currently." ;;
  esac

  case "${_system_arch}" in
    x86_64) arch=amd64 ;;
    *) rvm_error "TruffleRuby does not support ${_system_arch} currently." ;;
  esac

  rvm_ruby_package_name="truffleruby-${truffleruby_version}"

  if (( ${rvm_head_flag:=0} == 1 )); then
    case "$platform" in
      linux) platform="ubuntu-18.04" ;;
      macos) platform="macos-latest" ;;
    esac

    rvm_ruby_package_file="${rvm_ruby_package_name}-${platform}"
    rvm_ruby_url="${rvm_ruby_repo_url:-https://github.com/ruby/truffleruby-dev-builder/releases/latest/download/${rvm_ruby_package_file}.tar.gz}"
  else
    rvm_ruby_package_file="${rvm_ruby_package_name}-${platform}-${arch}"
    rvm_ruby_url="${rvm_ruby_repo_url:-$(__rvm_db "truffleruby_url")/vm-${truffleruby_version}/${rvm_ruby_package_file}.tar.gz}"
  fi

  true # for OSX
}

__rvm_select_interpreter_truffleruby()
{
  __rvm_truffleruby_set_version
  __rvm_truffleruby_set_rvm_ruby_url

  true # for OSX
}

__rvm_select_interpreter_maglev()
{
  rvm_ruby_patch_level=""
  maglev_url="$(__rvm_db "maglev_url")"

  system="${_system_type}"
  if [[ "$MACHTYPE" == x86_64-apple-darwin* ]]
  then arch="i386" # x86_64-apple-darwin supports both i386 and x86_64 maglev implements only i386
  else arch="${_system_arch}"
  fi

  if
    (( ${rvm_head_flag:=0} == 1 )) || [[ "$rvm_ruby_version" == "head" ]]
  then
    rvm_head_flag=1
    rvm_ruby_version="head"
    rvm_ruby_repo_url="${rvm_ruby_repo_url:-$(__rvm_db "maglev_repo_url")}"
    rvm_ruby_url="${rvm_ruby_repo_url:-$(__rvm_db "maglev_repo_url")}"
    rvm_gemstone_version=$(
      __rvm_curl -s https://raw.githubusercontent.com/MagLev/maglev/master/version.txt |
        __rvm_grep "^GEMSTONE" | cut -f2 -d-
    )
    rvm_gemstone_package_file="GemStone-${rvm_gemstone_version}.${system}-${arch}"
    rvm_disable_binary_flag=1
  else
    rvm_ruby_package_file="MagLev-${rvm_ruby_version}" # removed from 1.0: .${system}-${arch}
    rvm_ruby_version="${rvm_ruby_version:-"$(__rvm_db "maglev_version")"}"
    rvm_ruby_package_name="${rvm_ruby_interpreter}-${rvm_ruby_version}"
    rvm_ruby_url="${rvm_ruby_url:-"$maglev_url/${rvm_ruby_package_file}.${rvm_archive_extension}"}"
    rvm_gemstone_version=$(
      __rvm_curl -s https://raw.githubusercontent.com/MagLev/maglev/MagLev-${rvm_ruby_version}/version.txt |
        __rvm_grep "^GEMSTONE" | cut -f2 -d-
    )
    rvm_gemstone_package_file="GemStone-${rvm_gemstone_version}.${system}-${arch}"
  fi

  export MAGLEV_HOME="$rvm_ruby_home"
  export GEMSTONE_GLOBAL_DIR=$MAGLEV_HOME

  rvm_gemstone_url="$maglev_url/${rvm_gemstone_package_file}.${rvm_archive_extension}"

  true # for OSX
}

__rvm_select_interpreter_ironruby()
{
  rvm_ruby_patch_level=""
  if
    (( ${rvm_head_flag:=0} == 1 ))
  then
    rvm_ruby_version="head"
    rvm_ruby_package_name="${rvm_ruby_string}"
    rvm_ruby_repo_url="${rvm_ruby_repo_url:-$(__rvm_db "ironruby_repo_url")}"
    rvm_ruby_url="${rvm_ruby_repo_url:-$(__rvm_db "ironruby_repo_url")}"
    rvm_disable_binary_flag=1
  else
    rvm_archive_extension="zip"
    rvm_ruby_version=${rvm_ruby_version:-"$(__rvm_db "ironruby_version")"}
    rvm_ruby_package_name="${rvm_ruby_interpreter}-${rvm_ruby_version}"
    rvm_ruby_package_file="${rvm_ruby_interpreter}-${rvm_ruby_version}.${rvm_archive_extension}"
    rvm_ruby_url="$(__rvm_db "ironruby_${rvm_ruby_version}_url")"
  fi
  export rvm_ruby_version rvm_ruby_string rvm_ruby_package_name rvm_ruby_repo_url rvm_ruby_url rvm_archive_extension
  true # for OSX
}

__rvm_select_interpreter_ree()
{
  rvm_ruby_interpreter=ree
  rvm_ruby_version=${rvm_ruby_version:-"$(__rvm_db "ree_version")"}

  case "$rvm_ruby_version" in
    1.8.*) true ;; # all good!
    *) rvm_error "Unknown Ruby Enterprise Edition version: $rvm_ruby_version" ;;
  esac

  if [[ -n "${rvm_ruby_patch_level:-0}" ]]
  then rvm_ruby_patch_level="${rvm_ruby_patch_level#p}"
  fi

  rvm_ruby_package_file="ruby-enterprise-$rvm_ruby_version-$rvm_ruby_patch_level"
  rvm_ruby_url="$(__rvm_db "${rvm_ruby_interpreter}_${rvm_ruby_version}_${rvm_ruby_patch_level}_url")"
  rvm_ruby_url="${rvm_ruby_url:-$(__rvm_db "${rvm_ruby_interpreter}_${rvm_ruby_version}_url")}"
  rvm_ruby_url="${rvm_ruby_url}/$rvm_ruby_package_file.tar.gz"

  true # for OSX
}

__rvm_select_interpreter_common()
{
  rvm_ruby_interpreter="${1}"
  rvm_ruby_version="head"
  rvm_ruby_patch_level=""
  export rvm_head_flag=1
  rvm_ruby_repo_url="${rvm_ruby_repo_url:-$(__rvm_db "${1}_repo_url")}"
  rvm_ruby_url=$rvm_ruby_repo_url
  rvm_ruby_configure=""
  rvm_ruby_make=""
  rvm_ruby_make_install=""
}

__rvm_select_interpreter_opal()
{
  __rvm_select_interpreter_common "opal"
}

__rvm_select_interpreter_topaz()
{
  __rvm_select_interpreter_common "topaz"
}

__rvm_select_interpreter_mruby()
{
  rvm_ruby_interpreter="mruby"
  rvm_ruby_patch_level=""
  rvm_ruby_repo_url="${rvm_ruby_repo_url:-$(__rvm_db "mruby_repo_url")}"
  rvm_ruby_url=$rvm_ruby_repo_url
  rvm_ruby_configure=""
  rvm_ruby_make=""
  rvm_ruby_make_install=""
  export rvm_skip_autoreconf_flag=1
  if
    [[ -z "${rvm_ruby_version:-}" ]]
  then
    rvm_head_flag=1
  else
    rvm_head_flag=0
    rvm_archive_extension="tar.gz"
    rvm_ruby_package_file="${rvm_ruby_version}"
  fi
}

__rvm_select_interpreter_ruby()
{
  if
    [[ "${rvm_ruby_patch_level:-}" == "p0" ]] &&
    __rvm_version_compare "${rvm_ruby_version}" -ge 2.1.0 &&
    [[ ! -d "$rvm_rubies_path/$rvm_ruby_string" ]]
  then
    rvm_ruby_patch_level=""
    rvm_ruby_string="${rvm_ruby_string%-p0}"
  fi

  rvm_ruby_package_name="${rvm_ruby_interpreter}-${rvm_ruby_version}${rvm_ruby_patch_level:+-}${rvm_ruby_patch_level:-}"
  rvm_ruby_package_file="${rvm_ruby_package_name}"

  if
    [[ -z "${rvm_ruby_version:-""}" ]] &&
    (( ${rvm_head_flag:=0} == 0 ))
  then
    rvm_error "Ruby version was not specified!"
  else
    rvm_ruby_repo_url="${rvm_ruby_repo_url:-"$(__rvm_db "ruby_repo_url")"}"

    if
      (( ${rvm_head_flag:=0} == 0 ))
    then
      if __rvm_version_compare "${rvm_ruby_version}" -ge "3.0.0"
      then rvm_archive_extension="tar.gz"
      elif __rvm_version_compare "${rvm_ruby_version}" -lt "1.8.5"
      then rvm_archive_extension="tar.gz"
      else rvm_archive_extension="tar.bz2"
      fi
    else
      rvm_disable_binary_flag=1
    fi
  fi

  true # for OSX
}

__rvm_select_interpreter_ext()
{
  if
    [[ -z "${rvm_ruby_name:-${detected_rvm_ruby_name:-}}" ]]
  then
    rvm_error "External ruby name was not specified!"
    return 1
  fi
}

__rvm_select_interpreter_current()
{
  ruby_binary="$(builtin command -v ruby)"
  if
    (( $? == 0)) &&
    __rvm_string_match "$ruby_binary" "*rvm*"
  then
    rvm_ruby_string="$(dirname "$ruby_binary" | __rvm_xargs dirname | __rvm_xargs basename)"
  else
    rvm_ruby_interpreter="system"
  fi
}

__rvm_select_interpreter_default()
{
  true # do nothing
}

__rvm_select_interpreter_system()
{
  true # do nothing
}

__rvm_select_interpreter_user()
{
  true # do nothing
}

__rvm_select_interpreter_missing()
{
  return 2
}
